/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package window;

import busEvents.ChangeViewEvent;
import busEvents.OpenWaitingWindowEvent;
import com.adamtaft.eb.EventBusService;
import com.adamtaft.eb.EventHandler;
import data.Data;
import data.email.EmailMessage;
import enums.ViewEnum;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.*;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.ToolTipManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Arcanis
 */
public class MainFrame extends javax.swing.JFrame {

    private AutohideMouseAdapter hideAdapter;
    private HelpDialog h = new HelpDialog(this, false);

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        jTabbedPane1.remove(searchPanel2);
        JButton helpButton = new JButton(new javax.swing.ImageIcon(getClass().getResource("/icons/help-icon.png")));
        jToolBar1.add(helpButton);
        helpButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                h.setLocationRelativeTo(null);
                h.setVisible(true);
            }
        });              
        
        final Data data = new Data();     
        
        jButton1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                data.loadAccount();
                data.notifyObservers();
            }
        });
        
        this.setLookAndFeel("Windows");
        this.jToggleButtonWindows.setSelected(true);
        this.pack();
        EventBusService.subscribe(this);

        this.setSize(this.getSize().width, this.getSize().height + 1);

        hideAdapter = new AutohideMouseAdapter(jToolBar1, this);
        this.addMouseMotionListener(hideAdapter);
        this.addHiddenToolbarbehavior(jTabbedPane1);

        jToolBar1.setVisible(false);                        
        try {
            data.init();
        } catch (Exception ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        settings2.setAccountData(data.getReceiverAccountData(), data.getSenderAccountData());
        settings2.setData(data);
        contactsPanel2.setFolder(data.getContacts());
        data.addObserver(emailsPanel1);
        newEmailPanel2.setContactFolder(data.getContacts());
        newEmailPanel2.setData(data);      
        
        data.notifyObservers();           
    }

    private void addHiddenToolbarbehavior(Component component) {
        if (component instanceof Container) {
            for (Component comp : ((Container) component).getComponents()) {
                this.addHiddenToolbarbehavior(comp);
            }
        }
        component.addMouseMotionListener(hideAdapter);
    }

    @EventHandler
    public void handleChangeView(ChangeViewEvent event) {
        if (event.viewRequest.equals(ViewEnum.RECV_EMAILS)) {
            this.jTabbedPane1.setSelectedIndex(0);
        } else if (event.viewRequest.equals(ViewEnum.NEW_EMAIL)) {
            this.jTabbedPane1.setSelectedIndex(1);
        } else if (event.viewRequest.equals(ViewEnum.CONTACTS)) {
            this.jTabbedPane1.setSelectedIndex(2);
        } else if (event.viewRequest.equals(ViewEnum.SEARCH)) {
            this.jTabbedPane1.setSelectedIndex(3);
        } else if (event.viewRequest.equals(ViewEnum.SETTINGS)) {
            this.jTabbedPane1.setSelectedIndex(4);
        }
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jToggleButtonWindows = new javax.swing.JToggleButton();
        jToggleButtonHifi = new javax.swing.JToggleButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        emailsPanel1 = new window.EmailsPanel();
        newEmailPanel2 = new window.NewEmailPanel(this);
        contactsPanel2 = new window.ContactsPanel();
        searchPanel2 = new window.SearchPanel();
        settings2 = new window.SettingsPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("nas-mail");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                formWindowLostFocus(evt);
            }
        });

        jToolBar1.setFloatable(false);
        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setRollover(true);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/receive_mail.png"))); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);
        jToolBar1.add(jSeparator1);

        buttonGroup1.add(jToggleButtonWindows);
        jToggleButtonWindows.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/circle_grey.png"))); // NOI18N
        jToggleButtonWindows.setToolTipText("");
        jToggleButtonWindows.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jToggleButtonWindows.setFocusable(false);
        jToggleButtonWindows.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButtonWindows.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToggleButtonWindows.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonWindowsActionPerformed(evt);
            }
        });
        jToolBar1.add(jToggleButtonWindows);

        buttonGroup1.add(jToggleButtonHifi);
        jToggleButtonHifi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/circle_blue.png"))); // NOI18N
        jToggleButtonHifi.setFocusable(false);
        jToggleButtonHifi.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButtonHifi.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToggleButtonHifi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonHifiActionPerformed(evt);
            }
        });
        jToolBar1.add(jToggleButtonHifi);

        jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        jTabbedPane1.addTab("", new javax.swing.ImageIcon(getClass().getResource("/icons/E-mail-icon.png")), emailsPanel1); // NOI18N
        jTabbedPane1.addTab("", new javax.swing.ImageIcon(getClass().getResource("/icons/email-new-icon.png")), newEmailPanel2); // NOI18N
        jTabbedPane1.addTab("", new javax.swing.ImageIcon(getClass().getResource("/icons/Address_Book_Icon.png")), contactsPanel2); // NOI18N
        jTabbedPane1.addTab("", new javax.swing.ImageIcon(getClass().getResource("/icons/Search.png")), searchPanel2); // NOI18N
        jTabbedPane1.addTab("", new javax.swing.ImageIcon(getClass().getResource("/icons/cog-icon-2-48x48.png")), settings2); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 614, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
            .addComponent(jTabbedPane1)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jToggleButtonWindowsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonWindowsActionPerformed
        this.setLookAndFeel("Windows");
    }//GEN-LAST:event_jToggleButtonWindowsActionPerformed

    private void jToggleButtonHifiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonHifiActionPerformed
        this.setLookAndFeel("HiFi");
    }//GEN-LAST:event_jToggleButtonHifiActionPerformed

    private void formWindowLostFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowLostFocus
        jToolBar1.setVisible(false);
    }//GEN-LAST:event_formWindowLostFocus

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
//        EventBusService.publish(new ChangeViewEvent(ViewEnum.RECV_EMAILS));
        EventBusService.publish(new OpenWaitingWindowEvent("aaa"));
    }//GEN-LAST:event_jButton1ActionPerformed

    private void setLookAndFeel(String name) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                System.out.println(info);
                if (name.equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                }
            }
//            javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");            
//            javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
//            javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel");
            javax.swing.SwingUtilities.updateComponentTreeUI(this);
//            this.pack();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        javax.swing.UIManager.installLookAndFeel("HiFi", "com.jtattoo.plaf.hifi.HiFiLookAndFeel");
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private window.ContactsPanel contactsPanel2;
    private window.EmailsPanel emailsPanel1;
    private javax.swing.JButton jButton1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToggleButton jToggleButtonHifi;
    private javax.swing.JToggleButton jToggleButtonWindows;
    private javax.swing.JToolBar jToolBar1;
    private window.NewEmailPanel newEmailPanel2;
    private window.SearchPanel searchPanel2;
    private window.SettingsPanel settings2;
    // End of variables declaration//GEN-END:variables
}
